#============================================================
#	Platform Path
#============================================================
#  平台移动点路径
#============================================================
# @datetime: 2022-3-26 16:01:52
#============================================================

class_name ScanExternalPoint


signal path_completed


enum {
	ZERO = 0
	TOP = -1
	BOTTOM = 1
	LEFT = -1
	RIGHT = 1
}

const Direction = [
	# 从上方顺时针旋转移动
	Vector2(RIGHT, ZERO), Vector2(RIGHT, BOTTOM),
	Vector2(ZERO, BOTTOM), Vector2(LEFT, BOTTOM),
	Vector2(LEFT, ZERO), Vector2(LEFT, TOP),
	Vector2(ZERO, TOP), Vector2(RIGHT, TOP),
]


var _points : Dictionary = {}

var _external_points : Dictionary = {}
var _current_path_ponits : Array = []
var _external_point_group : Array = []
var _moved_points : Dictionary = {}


#============================================================
#   Set/Get
#============================================================
## 是否为边缘点
func is_external_point(pos: Vector2):
	# 方向条件：上/左/右方向不存在地板，则是边缘砖块
	return (
		not _points.has(pos + Vector2.UP)
		or not _points.has(pos + Vector2.LEFT)
		or not _points.has(pos + Vector2.RIGHT)
		or not _points.has(pos + Vector2.DOWN)
	)

func get_external_points() -> Array:
	return _external_points.keys()

func has_external_point(pos: Vector2) -> bool:
	return _external_points.has(pos)

func is_moved_point(pos: Vector2) -> bool:
	return _moved_points.has(pos)

func get_external_point_group() -> Array:
	return _external_point_group


#============================================================
#   自定义
#============================================================
## 开始扫描
func scan(points: Array):
	for pos in points:
		_points[pos] = null
	
	_scan_external_points()
	_scan_move_point()


## 扫描外部可触碰到的路径点
func _scan_external_points():
	for pos in _points:
		if is_external_point(pos):
			_external_points[pos] = null


## 扫描可移动路径点
func _scan_move_point():
	for point in get_external_points():
		_move(point)
		if _current_path_ponits.size() > 0:
			_external_point_group.append(_current_path_ponits)
			_current_path_ponits = []

## 移动到下一个位置
func _move(pos : Vector2):
	if not has_external_point(pos) or is_moved_point(pos):
		return
	
	_current_path_ponits.append(pos)
	_moved_points[pos] = null
	for dir in Direction:
		_move(pos + dir)
	


